package cn.itcast.anli.web;

import cn.itcast.anli.domain.PageBean;
import cn.itcast.anli.service.EmployeeService;
import cn.itcast.anli.service.impl.EmployeeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@WebServlet("/findEmpByPageServlet")
public class FindEmpByPageServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码
        request.setCharacterEncoding("utf-8");

        //获取条件查询的数据，封装成map集合
        Map<String, String[]> condition = request.getParameterMap();

        //为了方便，将当前页面数和每页显示数单独拿出来
        String currentPage = request.getParameter("currentPage");
        String row = request.getParameter("row");

        //如果直接访问/FindEmpByPageServlet，未携带相关参数数据，则获取该参数不能获得值
        if ( currentPage == null || "".equals(currentPage)){
            currentPage = "1";
        }
        if ( row == null || "".equals(row)){
            row = "5";
        }

        //将数据传递给service对象
        EmployeeService service = new EmployeeServiceImpl();
        PageBean empByPage = service.findEmpByPage(currentPage,row,condition);

        //请求转发
        //转发condition集合，以供查询表单中回写
        request.setAttribute("condition",condition);
        //转发page对象，一共列表页面显示员工列表、当前页面、总页面、总记录数
        request.setAttribute("page",empByPage);
        request.getRequestDispatcher("/list.jsp").forward(request,response);

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
